#include "FileNameParser.h"
#include "qstring.h"
#include "qstringlist.h"
#include <QtGui>

FileNameParser::FileNameParser(void)
{
}


FileNameParser::~FileNameParser(void)
{
}

QString FileNameParser::getFileFullName( QString & str)
{
	return str.mid(str.lastIndexOf("\\") +1,str.length() - str.lastIndexOf("\\") -1);
}



QString FileNameParser::getFileName( QString & str)
{
	if(str.lastIndexOf("\\") > str.lastIndexOf("."))
	{
		QMessageBox::about(0,"Invalid file path!","Invalid file path!");
		return "";
	}
	return str.mid(str.lastIndexOf("\\") +1,str.lastIndexOf(".") - str.lastIndexOf("\\") -1);
}

QString FileNameParser::getFileExtension( QString & str)
{
	return str.mid(str.lastIndexOf(".") ,str.length() - str.lastIndexOf(".") );
}

QStringList * FileNameParser::fileNameTransformer( QStringList & strl, const QString & targetPath ,bool rename )
{
	QStringList * strlret = new QStringList;
	QDateTime t;

	t = QDateTime::currentDateTime();



	QString fileNameUnique;
	fileNameUnique = t.toString(QString::fromLocal8Bit("yyyy-MM-dd"));
	for (int i = 0 ; i < strl.length(); i++)
	{
		QString fileFullName = getFileFullName(strl[i]);
		QString fileName = getFileName(fileFullName);
		QString filePath = getFilePath(strl[i]);
		QString fileExt = getFileExtension(fileFullName);
		QString newFileName;
		if(rename)
			newFileName = (targetPath == "" ? filePath:targetPath) + QString::number(i+1) + fileExt;
		else
		{

			newFileName = (targetPath == "" ? filePath:targetPath) + fileNameUnique + "." + QString::number(i+1);
		}
		strlret->append(newFileName);
		//QString fileExt = getFileExtension(fileFullName);
	}
	return strlret;
}

QString FileNameParser::getFilePath(const QString & str)
{
	return str.mid(0,str.lastIndexOf("\\")+1 );
}

